import { Component, OnInit } from '@angular/core';
import { Router,NavigationExtras,ActivatedRoute } from '@angular/router';
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
import { CommonResponse,VerificationType,ToastColor,CommonService,AppVerificationCode,AppParam } from '@services/common-service.service';
import { AuthTokenService } from '@services/auth-token.service';
import { environment } from 'src/environments/environment';
import { DefaultLanguageService } from '@services/default-language.service';
import {TranslateService} from '@ngx-translate/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';

@Component({
  selector: 'app-reset-password',
  templateUrl: './reset-password.page.html',
  styleUrls: ['./reset-password.page.scss'],
})
export class ResetPasswordPage implements OnInit {

  public loading:boolean = false;

  constructor(private router: Router,
    private route: ActivatedRoute,
    private commonService:CommonService,
    private authTokenService:AuthTokenService,
    private http:HttpClient,
    private defaultLanguageService:DefaultLanguageService,
    private translate:TranslateService) { }

  public defaultSmsCountDown:number;

  public smsCountDown:number;

  public sendView:boolean = true;

  private i18n:any = {};

  public validateForm:FormGroup;
  ngOnInit() {
    this.initLanguage();
    this.defaultSmsCountDown = parseInt(this.commonService.getAppParamFromCache(AppParam.SMS_COUNT_DOWN));
    this.smsCountDown = this.defaultSmsCountDown; 
    this.validateForm = new FormGroup({
      'verificationCode': new FormControl('', [
        Validators.required
      ])
    });
    this.sendAppVerificationCode();
  }

  get verificationCode(){return this.validateForm.get('verificationCode');}
  
  initLanguage(){
    this.i18n = this.commonService.initTranslate([]); 
  }

  sendAppVerificationCode(){
    if(!this.authTokenService.getAppUserId()){
      return false;
    }
    let sendAppVerificationCode:AppVerificationCode = {
      appUserId:this.authTokenService.getAppUserId(),
      mobileNumber:'',
      verificationType:VerificationType.UPDATE_USER_PASSWORD_AFTER_LOGIN,
      verificationCode:'',
      appCode:environment.APP_CODE
    }
    this.commonService.sendAppVerificationCode(sendAppVerificationCode)
    .subscribe((response:CommonResponse)=>{
      this.smsCountDown = this.defaultSmsCountDown;
      this.sendView = true;
      var id = setInterval(() => {
        this.smsCountDown--;
        if(this.smsCountDown == 0){
          clearInterval(id);
          this.sendView = false;
        }
      }, 1000);
    },(errorResponse:HttpErrorResponse)=>{
      this.commonService.errorRespCommonToast(errorResponse,this.i18n);
    })
  }

  checkAppVerificationCode(){
    if(this.validateForm.invalid){
      for (const i in this.validateForm.controls) {
        this.validateForm.controls[i].markAsDirty();
        this.validateForm.controls[i].updateValueAndValidity();
      }
      return false;
    }
    this.loading = true;
    let checkAppVerificationCode:AppVerificationCode = {
      appUserId:this.authTokenService.getAppUserId(),
      verificationCode:this.validateForm.value.verificationCode,
      verificationType:VerificationType.UPDATE_USER_PASSWORD_AFTER_LOGIN,
      mobileNumber:'',
      appCode:environment.APP_CODE
    }
    this.commonService.checkAppVerificationCode(checkAppVerificationCode).subscribe((response:CommonResponse)=>{
      if(response.isSuccess && response.responseBody.isPass){
        this.router.navigate(['/tabs/myAccount/setting/account-security/login-password/reset-password/reset-password-entry']);
      }else{
        this.commonService.commonToast(ToastColor.warning);
      }
      this.loading = false;
    },(errorResponse:HttpErrorResponse)=>{
      this.commonService.errorRespCommonToast(errorResponse,this.i18n);
      this.loading = false;
    });
  }

}
